package org.aceor.mddal.gateway.util;

import org.aceor.mddal.common.sqlparser.result.object.AggregationColumn;
import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.merger.function.AggregateFunction;
import org.aceor.mddal.gateway.merger.function.aggregate.*;

/**
 * Created by lxue on 16/4/17.
 */
public class AggregateFunctionUtil {
    public static AggregateFunction createAggregateFunction(AggregationColumn.AggregationType aggregationType, Object value) {
        AggregateFunction function;
        DataType datatype = DataTypeUtil.getTypeOfObject(value);
        switch (aggregationType) {
            case MAX:
                function = new Max(datatype);
                break;
            case MIN:
                function = new Min(datatype);
                break;
            case SUM:
                function = new Sum(datatype);
                break;
            case COUNT:
                function = new Count(datatype);
                break;
            case AVG:
                function = new Avg(datatype);
                break;
            default:
                throw new IllegalArgumentException("不支持该聚集函数类型 :"+aggregationType.toString());
        }
        return function;
    }
}
